package com.xiaotu.spider.qq.pageprocessor;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.SgWxModel;
import com.xiaotu.common.util.*;
import com.xiaotu.spider.APageProcessor;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.selector.Html;

import java.util.*;

/**
 * @类名 TVSgWxPageProcessor
 * @日期 2017年3月3日
 * @作者 高海军
 * @功能 电视剧搜狗微信数据爬取
 */
@Component("TVSgWxPageProcessor")
@Scope("prototype")
public class TVSgWxPageProcessor extends APageProcessor
{
    private static final Logger LOGGER = LoggerFactory.getLogger(TVSgWxPageProcessor.class);

    /*private static final String SEARCH_URL = "http://weixin.sogou" +
            ".com/weixin?type=2&ie=utf8&query=%s&tsn=2&ft=&et=&interation=&wxid=&usip=";*/
    private static final String SEARCH_URL = "http://weixin.sogou" +
            ".com/weixin?type=2&s_from=input&query=%s&ie=utf8&_sug_=n&_sug_type_=";

    public static final String PATH_PARAM = "filePath";

    private String filePath;

    @Autowired
    protected CacheHandler cacheHandler;

    @Override
    public void initRunParams(Map<String, Object> map)
    {
        if (map.get(PATH_PARAM) == null)
            throw new SpiderException(SpiderExceptionCode.PageProcessorParameterErrorException);
        this.filePath = map.get(PATH_PARAM).toString();
    }

    @Override
    protected Request getStartRequest(JSONObject target)
    {
        return new Request(String.format(SEARCH_URL, KeywordEncode.encode(target.getString(this
                .getNameField()))));
    }

    protected String getNameField()
    {
        return "tvName";
    }

    protected String getIdField()
    {
        return "tvid";
    }

    protected String getTargetType()
    {
        return Constants.TargetType.TV;
    }

    @Override
    public void process(Page page)
    {
        if (Constants.PageDataType.SEARCH_LIST.equals(this.getPageDataType(page)))
            this.listProcess(page);
        else
            this.pageProcess(page);
    }

    private void listProcess(Page page)
    {
        Html html = page.getHtml();
        List<String> divList = html.xpath("//ul[@class='news-list']//div[@class='txt-box']").all();
        if (divList == null || divList.isEmpty())
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);

        divList.forEach(divStr ->
        {
            Html divObj = new Html(divStr);
            String date = divObj.xpath("//div[@class='s-p']/span/script").regex(RegexUtils
                    .REGEX_INTEGER).get();
            if (StringUtils.isEmpty(date))
            {
                LOGGER.error("[{}] article date is empty:{}", this.getSpiderType(), page.getUrl()
                        .toString());
                return;
            }
            date = DateUtils.transferLongToDateString(DateUtils.DAY_FORMAT, Long.parseLong(date)
                    * 1000);
            String title = StringUtil.toSBC(divObj.xpath("//div[@class='txt-box']/h3/a/text()")
                    .get());
            String source = divObj.xpath("//div[@class='s-p']/a/text()").get();
            String field = DataRedisKeyUtils.getSgWxKey(new SgWxModel(page.getRequest().getExtra
                    (this.getIdField()).toString(), null, this.getTargetType(), title, date,
                    source, null));
            if (cacheHandler.hExists(Constants.DataRedisKey.Sougo_WX_History.toString(), field))
                return;
            PageUtils.addTargetRequest(page, divObj.xpath("//div[@class='txt-box']/h3/a/@href")
                    .get(), page.getRequest().getExtras());
        });

        PageUtils.addTargetRequest(page, html.xpath("//a[@id='sogou_next']/@href").get(), page
                .getRequest().getExtras());
    }

    private void pageProcess(Page page)
    {
        Html html = page.getHtml();
        String title = null,date = null,source = null,content=null;
        /*List<String> titles = html.xpath("//div[@id='img-content']/h2[@id='activity-name']/").all();
        if(titles.size()>0){
        	title =titles.toString().substring(titles.toString().indexOf("else{")+5, titles.toString().indexOf("</script>"));
        	title =StringUtils.trim(title.substring(title.indexOf(".write(\"")+8,title.indexOf("\");")));
        }*/
        if(html.toString().contains("var msg_title")&&html.toString().contains("var msg_desc")){
        	title =html.toString().substring(html.toString().indexOf("var msg_title")+13, html.toString().indexOf("var msg_desc"));
        	title =StringUtils.trim(title.substring(title.indexOf("\"")+1,title.indexOf("\";")));
        }
        if(html.toString().contains("var publish_time")&&html.toString().contains("var user_name")){
        	date =html.toString().substring(html.toString().indexOf("var publish_time")+16, html.toString().indexOf("var user_name"));
        	date =StringUtils.trim(date.substring(date.indexOf("\"")+1,date.indexOf("\" ||")));
        }
        if(html.toString().contains("var nickname")&&html.toString().contains("var appmsg_type")){
        	source =html.toString().substring(html.toString().indexOf("var nickname")+12, html.toString().indexOf("var appmsg_type"));
        	source =StringUtils.trim(source.substring(source.indexOf("\"")+1,source.indexOf("\";")));
        }
        if(html.toString().contains("msg_desc")&&html.toString().contains("var msg_cdn_url")){
        	content =html.toString().substring(html.toString().indexOf("var msg_desc")+12, html.toString().indexOf("var msg_cdn_url"));
        	content =StringUtils.trim(content.substring(content.indexOf("\"")+1,content.indexOf("\";")));
        }
       /* String title = StringUtils.trim(html.xpath("//h2[@id='activity-name']/text()").get());
        title = StringUtils.isEmpty(title) ? StringUtils.trim(html.xpath
                ("//div[@class='original_panel_title']/text()").get()) : title;
        String date = StringUtils.trim(html.xpath("//em[@id='post-date']/text()").get());
        date = StringUtils.isEmpty(date) ? StringUtils.trim(html.xpath
                ("//span[@id='publish_time']/text()").get()) : date;
        String source = StringUtils.trim(html.xpath("//a[@id='post-user']/text()").get());
        source = StringUtils.isEmpty(source) ? StringUtils.trim(html.xpath
                ("//div[@id='post-user']/strong/text()").get()) : source;
        if (StringUtils.isEmpty(title) || StringUtils.isEmpty(date) || StringUtils.isEmpty(source))
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        String content = HtmlUtil.delHTMLTag(html.xpath("//div[@id='js_content']").get());*/
        page.putField(SgWxModel.class.getName(), new SgWxModel(page.getRequest().getExtra(this
                .getIdField()).toString(), page.getRequest().getExtra(this.getNameField())
                .toString(), this.getTargetType(), title, date, source, content));
        page.putField(PATH_PARAM, this.filePath);
        page.putField("html", page.getRawText());
    }

    @Override
    public String getSpiderType()
    {
        return "TVSgWx";
    }

    @Override
    public String getPageDataType(Page page)
    {
        String url = page.getUrl().toString();
        if (url.contains("weixin.sogou.com"))
            return Constants.PageDataType.SEARCH_LIST;
        return Constants.PageDataType.TV_COMMENT;
    }
}
